package cyan.gf.field;


import cyan.gf.db.Column;
import cyan.gf.db.TableField;
import cyan.gf.util.NameUtil;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by cyan on 2017/2/2.
 * Email:qsyan@vip.qq.com
 */
public class MySQLFieldWarpper implements FieldWarpper {

  public TableField warpperd(ResultSet rs) throws SQLException {
    String key, extra, fieldText, typeText, nullable, comment;
    fieldText = rs.getString(1);
    typeText = rs.getString(2);
    nullable = rs.getString(4);
    key = rs.getString(5);
    extra = rs.getString(7);
    comment = rs.getString(9);
    TableField tableField = new TableField();
    tableField.setFieldText(fieldText);
    tableField.setFieldName(NameUtil.asFieldName(fieldText));
    tableField.setTypeText(typeText);
    tableField.setComment(comment);
    tableField.setFunName(NameUtil.asClassName(fieldText));
    if (nullable.equals("YES")) {
      tableField.setNullable("true");
    } else {
      tableField.setNullable("false");
    }
    switch (key) {
      case "PRI":
        tableField.setKey("Id");
        break;
      default:
        tableField.setKey("Basic");
    }
    switch (extra) {
      case "auto_increment":
        tableField.setExtra(Column.auto_increment);
        break;
    }
    if (typeText.contains("char") | typeText.contains("text")) {
      tableField.setType("String");
    } else if (typeText.contains("int")) {
      tableField.setType("Integer");
    } else if (typeText.contains("date") | typeText.contains("time")) {
      tableField.setType("Long");
    } else if (typeText.contains("blob")) {
      tableField.setType("Byte[]");
    } else if (typeText.contains("double")) {
      tableField.setType("Double");
    } else if (typeText.contains("float")) {
      tableField.setType("Float");
    } else {
      tableField.setType("Object");
    }
    return tableField;
  }
}
